﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BST250301TestStand.Models
{
    public class AppDbContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Project> Projects { get; set; }
        public DbSet<Parameter> Parameters { get; set; }
        public DbSet<ProjectParameter> ProjectParameters { get; set; }
        public DbSet<Template> Templates { get; set; }
        public DbSet<TemplateProject> TemplateProjects { get; set; }
        public DbSet<TemplateProjectParameter> TemplateProjectParameters { get; set; }

        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // Unique constraints
            modelBuilder.Entity<User>().HasIndex(u => u.Name).IsUnique();
            modelBuilder.Entity<Project>().HasIndex(p => p.Name).IsUnique();

            // Composite unique keys
            modelBuilder.Entity<ProjectParameter>()
                .HasIndex(pp => new { pp.ProjectId, pp.ParameterId })
                .IsUnique();

            modelBuilder.Entity<TemplateProjectParameter>()
                .HasIndex(tpp => new { tpp.TemplateProjectId, tpp.ParameterId })
                .IsUnique();
        }
    }
}
